package over;

import java.lang.reflect.Method;
import java.util.List;

public class Jcall implements JStmt{

    String x;
    JExpr r;
    String m;
    List<JExpr> args;

    @Override
    public void eval(Env env) {
        try {
            Object[] values = new Object[args.size()];
            Class<?>[] classes = new Class<?>[args.size()];
            for (int i = 0; i < args.size(); i++) {
                JExpr jExpr = args.get(i);
                values[i] = jExpr.eval(env);
                classes[i] = jExpr.eval(env).getClass();
            }

            Object object = r.eval(env);
            Method method = r.getClass().getMethod(m, classes);

            env.update(x, method.invoke(object, values));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
